package baidu;

import java.math.BigDecimal;
import java.util.Scanner;

public class one {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int str1[] = new int[n];
        for (int i = 0; i < n; i++) {
            str1[i] = scan.nextInt();
        }
        int count0 = 0;
        int count5 = 0;
        for (int i = 0; i < str1.length; i++) {
            if (str1[i]==0){
                count0++;
            }else{
                count5++;
            }
        }
        if (count0==0||count5<9){
            System.out.println(-1);
        }else{
            String s = "";
            int i = count5/9;
            String s1 = "555555555";
            for (int j = 0; j < i; j++) {
                s+=s1;
            }
            for (int j = 0; j < count0; j++) {
                s+=0;
            }
            System.out.println(s);
        }
    }
}
